package com.ff.dao;

import static com.ff.mapper.FuserDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import java.util.List;
import java.util.Optional;

import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ff.mapper.FuserMapper;
import com.ff.model.Fuser;

@Repository
public class FuserDao {
	@Autowired
	private FuserMapper fmapper;
	public void setFmapper(FuserMapper fmapper) {
		this.fmapper = fmapper;
	}
	
	public Fuser login(String username1, String upassword1) {//首页登录
		SelectStatementProvider fuser1=select(fmapper.selectList)
										.from(fuser)
										.where(fusername,isEqualTo(username1))
										.and(fpassword, isEqualTo(upassword1))
								 		.build()
								 		.render(RenderingStrategies.MYBATIS3);
				Optional<Fuser> opt=fmapper.selectOne(fuser1);
		return opt.isPresent()?opt.get():null;
	}
	
	public int register1(Fuser fuser) {//注册账号
		
		return fmapper.insert(fuser);
	}
	public int updateupd(int fid1, String username1, String upassword1) {//修改密码
		UpdateStatementProvider u=update(fuser)
									.set(fpassword).equalTo(upassword1)
									.where(fid,isEqualTo(fid1))
									.and(fusername,isEqualTo(username1))
									.build()
									.render(RenderingStrategies.MYBATIS3);
		return 0;
	}
	public int update1(Fuser fuser) {//修改用户数据
		return fmapper.updateByPrimaryKey(fuser);
	}
	public int delete1(int uid) {//删除用户数据
		return fmapper.deleteByPrimaryKey(uid);
	}
	public List<Fuser> select_() {//查询所有用户
		SelectStatementProvider ssp=select(fmapper.selectList)
				.from(fuser)
				.where(power,isNotEqualTo(0))
				.build()
				.render(RenderingStrategies.MYBATIS3);
			List<Fuser> opt=fmapper.selectMany(ssp);
			return opt!=null?opt:null;
	}
	public List<Fuser> select_limit(int id2, int cnt1) {//分页查询用户
		SelectStatementProvider ssp=select(fmapper.selectList)
				.from(fuser)
				.where(power,isNotEqualTo(0))
				.limit(cnt1).offset(id2)
				.build()
				.render(RenderingStrategies.MYBATIS3);
		List<Fuser> opt=fmapper.selectMany(ssp);
		return opt!=null?opt:null;
	}
	
	
	public Fuser find11(int id) {//id查询用户
		SelectStatementProvider s=select(fmapper.selectList)
								.from(fuser)
								.where(fid,isEqualTo(id))
								.build()
								.render(RenderingStrategies.MYBATIS3);
		Optional<Fuser> opt=fmapper.selectOne(s);
		return opt.isPresent()?opt.get():null;
	}
	
	
}
